package com.application.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.application.domain.Activity;
import com.application.domain.Community;
import com.application.service.ActivityMemberService;
import com.application.service.ActivityService;
import com.application.service.CommunityService;
import com.application.util.RedisIdWorker;
import io.swagger.models.auth.In;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.application.util.RedisConstant.ACTIVITY_NUMS_KEY;
import static com.application.util.RedisConstant.ACTIVITY_NUMS_SET_KEY;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private CommunityService communityService;

    @Resource
    private ActivityService activityService;

    @Resource
    private RedisIdWorker redisIdWorker;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ActivityMemberService activityMemberService;


    @GetMapping("/login")
    public Result testLogin(){
        return new Result(200,null,"测试成功");
    }

    @GetMapping("/queryCommunityById")
    public Result queryCommunityById(@RequestParam Integer id){
        Community community = communityService.queryById(id);
        String mes = community == null ? "没有数据" :"查询成功";
        Integer code = community == null ? 500 : 200;
        return new Result(code,community,mes);
    }

    @GetMapping("/getAllCommunity")
    public Result getAllCommunity(){
        List<Community> communities = communityService.selectAll();
        String mes = communities == null ? "请重新进入界面" :"查询成功";
        Integer code = communities == null ? 500 : 200;
        return new Result(code,communities,mes);
    }


    @PostMapping("/updateCommunity")
    public Result updateCommunity(@RequestBody Community community){
        return communityService.updateCommunity(community);
    }

    @PostMapping("/updateActivity")
    public Result updateActivity(@RequestBody Activity activity){
        return activityService.updateCommunity(activity);
    }

    @GetMapping("/textRedisIdWorker")
    public Result textRedisIdWorker(@RequestParam String keyPrefix){
        long id = redisIdWorker.nextId("activity");
        return new Result(200,id,"获取id成功");
    }

    @PostMapping("/insertActivity2Redis")
    public Result insertActivity2Redis(@RequestParam Integer activityId){
        // 首先  就是存放到redis包括  人数 和 活动集合
        Activity activity = activityService.getById(activityId);

        // 这里就是  将 活动的人数存放到redis
        Integer number = activity.getNumber();

        stringRedisTemplate.opsForValue().set(ACTIVITY_NUMS_KEY + activity.getId(),number.toString());
        stringRedisTemplate.opsForSet().add(ACTIVITY_NUMS_SET_KEY + activityId,"9");

        return new Result(200,null,"操作成功");
    }

    @PostMapping("/testApplyActivity")
    public Result testApplyActivity(@RequestParam String activityId){
        return activityMemberService.applyActivity(activityId);
    }
}
